package com.icare.shop.listener;

import com.sun.org.apache.xalan.internal.Version;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;


@Order(Integer.MIN_VALUE)
public class ShopBannerApplicationListener
    implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
  private static final Log logger = LogFactory.getLog(ShopBannerApplicationListener.class);

  private static Mode BANNER_MODE = Mode.CONSOLE;

  public static void setBANNER_MODE(Mode bANNER_MODE) {
    BANNER_MODE = bANNER_MODE;
  }

  @Override
  public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    // don't listen to events in a bootstrap context
    //issues :https://github.com/spring-cloud/spring-cloud-commons/issues/78
    if(event.getEnvironment().getPropertySources().contains("bootstrap")){
      return;
    }
    if (BANNER_MODE == Mode.OFF) {
      return;
    }
    String bannerText = this.buildBannerText();
    if (BANNER_MODE == Mode.CONSOLE) {
      System.out.print(bannerText);
    } else if (BANNER_MODE == Mode.LOG) {
      logger.info(bannerText);
    }
  }

  private String buildBannerText() {
    StringBuilder bannerTextBuilder = new StringBuilder();
    bannerTextBuilder.append("\n").append(LOGO)
        .append(" :: Shop Config ::        (v").append(Version.getVersion()).append(")")
        .append("\n");
    return bannerTextBuilder.toString();
  }

  public static final String LOGO =("\n")
          +("					_ooOoo_                   \n")
          +("				   o8888888o                  \n")
          +("				   88\" . \"88                  \n")
          +("				   (| -_- |)                  \n")
          +("				   O\\  =  /O                  \n")
          +("				____/`---'\\____               \n")
          +("			  .'  \\|     |//  `.             \n")
          +("			 /  \\\\|||  :  |||//  \\            \n")
          +("			/  _||||| -:- |||||-  \\           \n")
          +("			|   | \\\\\\  -  /// |   |           \n")
          +("			| \\_|  ''\\---/''  |   |           \n")
          +("			\\  .-\\__  `-`  ___/-. /           \n")
          +("		  ___`. .'  /--.--\\  `. . __          \n")
          +("	   .\"\" '<  `.___\\_<|>_/___.'  >'\"\".       \n")
          +("	  | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |     \n")
          +("	  \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /     \n")
          +(" ======`-.____`-.___\\_____/___.-`____.-'======\n")
          +("                    `=---='                   \n")
          +(" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n")
          +("			佛祖保佑		永无BUG                 \n");

}
